package _designPatterm.代理模式;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKDynamicProxyDemo {
    static interface IService {
        void sayHello();
    }

    static class RealService implements IService {

        @Override
        public void sayHello() {
            System.out.println("hello,O(∩_∩)O");
        }
    }

    static class SimpleInvocationHandler implements InvocationHandler {

        private Object realObJ;

        public SimpleInvocationHandler(Object realObJ) {
            this.realObJ = realObJ;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("entering" + method.getName());
            Object result = method.invoke(realObJ, args);
            System.out.println("leaving" + method.getName());
            return result;
        }

    }

    public static void main(String[] args) {
        IService realService = new RealService();
        IService proxyService = (IService) Proxy.newProxyInstance(IService.class.getClassLoader(), new Class[]{IService.class},
                new SimpleInvocationHandler(realService));
        proxyService.sayHello();
    }

}
